package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SiyuRecharge;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISiyuRechargeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 充值记录Controller
 */
@RestController
@RequestMapping("/system/recharge")
public class SiyuRechargeController extends BaseController {

    @Autowired
    private ISiyuRechargeService siyuRechargeService;

    /**
     * 查询充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:recharge:list')")
    @GetMapping("/list")
    public TableDataInfo list(SiyuRecharge siyuRecharge) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!user.isAdmin()) {
            siyuRecharge.setUserId(user.getUserId());
        }
        startPage();
        List<SiyuRecharge> list = siyuRechargeService.selectSiyuRechargeList(siyuRecharge);
        return getDataTable(list);
    }

    /**
     * 新增充值记录
     */
    @PreAuthorize("@ss.hasPermi('system:recharge:add')")
    @Log(title = "充值记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SiyuRecharge siyuRecharge) {
        return toAjax(siyuRechargeService.insertSiyuRecharge(siyuRecharge));
    }

    /**
     * 轮询支付订单
     */
    @GetMapping(value = "/lunxun/{outTradeNo}")
    public AjaxResult getInfo(@PathVariable("outTradeNo") String outTradeNo) {
        SiyuRecharge siyuRecharge = siyuRechargeService.selectSiyuRechargeList(new SiyuRecharge(outTradeNo)).get(0);
        if (siyuRecharge != null && "1".contains(siyuRecharge.getStatus())) {
            return success("ok");
        } else {
            return success("no");
        }
    }

    /**
     * 导出充值记录列表
     */
    @PreAuthorize("@ss.hasPermi('system:recharge:export')")
    @Log(title = "充值记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SiyuRecharge siyuRecharge) {
        List<SiyuRecharge> list = siyuRechargeService.selectSiyuRechargeList(siyuRecharge);
        ExcelUtil<SiyuRecharge> util = new ExcelUtil<SiyuRecharge>(SiyuRecharge.class);
        util.exportExcel(response, list, "充值记录数据");
    }

}
